﻿using System;

namespace RockBus.Transport.Db
{
    public interface IDbTransportConfiguration
    {
        string Id { get; }

        int MessageFragmentSize { get; }

        int PollingBatchSize { get; }

        int PollingIntervalMs { get; }

        int DeliveryTimeout { get; }

        int DefaultMaxDeliveryAttemptCount { get; }

        TimeSpan DefaultDeliveryAttemptInterval { get; }

        long DefaultDeliveryAttemptIntervalSecs { get; }

        TimeSpan DefaultMessageValidityDuration { get; }

        long? DefaultMessageValidityDurationSecs { get; }

        int? DefaultMessageBufferMaxCount { get; }
    }

    public class DbTransportConfigurationImpl : IDbTransportConfiguration
    {
        public DbTransportConfigurationImpl()
        {
            //this.DefaultDeliveryAttemptInterval = DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultDeliveryAttemptInterval;
            //this.DefaultDeliveryAttemptIntervalSecs = (long) DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultDeliveryAttemptInterval.TotalSeconds;
            //this.DefaultMessageValidityDuration = DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageValidityDuration;
            //this.DefaultMessageValidityDurationSecs = (DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageValidityDuration == TimeSpan.Zero) ?
            //    null : (long?) DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageValidityDuration.TotalSeconds;
            //this.DefaultMessageBufferMaxCount = (DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageBufferMaxCount <= 0) ?
            //    null : (int?) DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageBufferMaxCount;
        }

        public string Id { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.Id; } }

        public int MessageFragmentSize { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.MessageFragmentSize; } }

        public int PollingBatchSize { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.PollingBatchSize; } }

        public int PollingIntervalMs { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.PollingIntervalMs; } }

        public int DeliveryTimeout { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.DeliveryTimeout; } }

        public int DefaultMaxDeliveryAttemptCount { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMaxDeliveryAttemptCount; } }

        public TimeSpan DefaultDeliveryAttemptInterval { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultDeliveryAttemptInterval; } }

        public long DefaultDeliveryAttemptIntervalSecs { get { return (long)DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultDeliveryAttemptInterval.TotalSeconds; } }

        public TimeSpan DefaultMessageValidityDuration { get { return DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageValidityDuration; } }

        public long? DefaultMessageValidityDurationSecs
        {
            get
            {
                return (DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageValidityDuration == TimeSpan.Zero) ?
                    null : (long?)DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageValidityDuration.TotalSeconds;
            }
        }

        public int? DefaultMessageBufferMaxCount
        {
            get
            {
                return (DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageBufferMaxCount <= 0) ?
                    null : (int?)DbTransportConfigurationSection.Instance.DbTransportConfiguration.DefaultMessageBufferMaxCount;
            }
        }
    }
}